/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CDU6.java
 *
 * Created on 05-oct-2009, 12:33:37
 */

package tpgrandt.UI;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.table.TableColumn;
import tpgrandt.control.Listado;
import tpgrandt.UI.model.ListadoFechaModel;

/**
 *
 * @author k3nsou
 */
public class GanadoresDeLaFecha extends javax.swing.JInternalFrame implements MouseListener{

    private Listado listado;
    private ListadoFechaModel participantes;
    private String fechasCerradas[];
    private Vector<String[]> datos;
    private JLabel[] indices;
    int paginas;
    int pagSeleccionada;

    /** Creates new form CDU6 */
    public GanadoresDeLaFecha(Listado listado) {
        super("Ganadores de la fecha", false, true, false, false);      
        
        initComponents(); 
        
        Vector<Integer> aux = listado.fechasCerradas();
        fechasCerradas = new String[aux.size()];
        for(int i = 0; i < aux.size(); i++)
            fechasCerradas[i] = aux.elementAt(i).toString();
        
        jComboBox2.setModel(new DefaultComboBoxModel(fechasCerradas));


        this.listado = listado;

        jComboBox2ActionPerformed(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel18 = new javax.swing.JLabel();
        jFormattedTextField2 = new javax.swing.JFormattedTextField();
        jFormattedTextField3 = new javax.swing.JFormattedTextField();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        participantes = new ListadoFechaModel(null);
        jTable1.setModel(participantes);
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        TableColumn tc = jTable1.getColumn("Posición");
        tc.setPreferredWidth(50);
        tc = jTable1.getColumn("Apellido y nombres");
        tc.setPreferredWidth(150);
        tc = jTable1.getColumn("Equipo de fantasía");
        tc.setPreferredWidth(150);
        tc = jTable1.getColumn("Puntos");
        tc.setPreferredWidth(45);
        tc = jTable1.getColumn("Jugador estrella (club)");
        tc.setPreferredWidth(220);

        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Ver listado de impresión");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel16.setText("Posición:");

        jLabel17.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel17.setText("Puntos:");

        jComboBox2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jComboBox2MouseClicked(evt);
            }
        });
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jLabel18.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel18.setText("Fecha:");

        jFormattedTextField2.setEditable(false);
        jFormattedTextField2.setText("< Datos pertinentes>");

        jFormattedTextField3.setEditable(false);
        jFormattedTextField3.setText("< Datos pertinentes>");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 677, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 21, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel18)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(69, 69, 69)
                        .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFormattedTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(57, 57, 57)
                        .addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                        .addComponent(jFormattedTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 677, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jFormattedTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jFormattedTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        int fecha = Integer.parseInt(jComboBox2.getSelectedItem().toString().trim());
        datos = listado.listadoFecha(fecha);

        int posicion = listado.buscar();
        
        if (posicion != -1) {
            jFormattedTextField3.setText(datos.elementAt(posicion)[3]);
            jFormattedTextField2.setText(new Integer(posicion + 1).toString());
        } else {
            jFormattedTextField3.setText("-");
            jFormattedTextField2.setText("-");
        }


        paginas = (int) Math.ceil((double) datos.size() / 10);
        //paginas = paginas > 10 ? 10 : paginas;

        
        if (indices != null)
	    for(int i = 0; i < indices.length; i++) jPanel1.remove(indices[i]);

	indices = new JLabel[paginas];
        for (int i = 0; i < paginas; i++) {
            indices[i] = new javax.swing.JLabel();
            indices[i].setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
            indices[i].setFont(new java.awt.Font("Tahoma", 1, 14));
            indices[i].setForeground(new java.awt.Color(0, 102, 255));
            indices[i].setAlignmentY(JLabel.CENTER_ALIGNMENT);
            String tmp = " " + (i + 1) + (i + 1 > 9 ? "" : " ");
            indices[i].setText(tmp);
            indices[i].addMouseListener(this);
            jPanel1.add(indices[i]);
        }

        pagSeleccionada = 1;
        if (paginas != 0) {
            mouseClicked(new MouseEvent(new JLabel("1"), 0, 0, 0, 0, 0, 0, false));
        } else {
            if (participantes != null)
                participantes.cambiarLista(new Vector<String[]>());
            else
                participantes = new ListadoFechaModel(new Vector<String[]>());
            jTable1.updateUI();
            jPanel1.updateUI();
        }
}//GEN-LAST:event_jComboBox2ActionPerformed

    private void jComboBox2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jComboBox2MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox2MouseClicked

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jTable1MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JFormattedTextField jFormattedTextField2;
    private javax.swing.JFormattedTextField jFormattedTextField3;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    public void mouseClicked(MouseEvent e) {
        JLabel comp = (JLabel) e.getComponent();

        indices[pagSeleccionada - 1].setFont(new java.awt.Font("Tahoma", 1, 14));
        pagSeleccionada = Integer.parseInt(comp.getText().trim());
        indices[pagSeleccionada - 1].setFont(new java.awt.Font("Tahoma", 1, 16));

        Vector<String[]> a = new Vector<String[]>();
        for (int j = 0; j < 10 && j < datos.size(); j++) {
            a.add(datos.elementAt(((pagSeleccionada - 1) * 10) + j));
        }

        if (participantes != null)
            participantes.cambiarLista(a);
        else
            participantes = new ListadoFechaModel(a);
        jPanel1.updateUI();
	jTable1.updateUI();
    }

    public void mousePressed(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
